import interpolation

def get_options():
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option('-i', '--input',      dest='input',       type='string',                 help='location of stellar observations')
    parser.add_option('-f', '--format',     dest='format',      type='string', default='.dat', help='file format')
    parser.add_option('-o', '--output',     dest='output',      type='string',                 help='desired output')
    parser.add_option('--interpolant',      dest='interpolant', type='string', default='poly', help='type of interpolation (poly, spline, trig, lin)')
    parser.add_option('-d', '--degree',     dest='degree',      type='int',    default=5,      help='degree of interpolation')
    parser.add_option('--min_obs',          dest='min_obs',     type='int',    default=100,    help='minimum number of observations per star')
    parser.add_option('--period_bins',      dest='period_bins', type='int',    default=50000,  help='the size of the period space')
    parser.add_option('--min_period',       dest='min_period',  type='float',  default=0.2,    help='minimum period of each star')
    parser.add_option('--max_period',       dest='max_period',  type='float',  default=32.,    help='maximum period of each star')
    parser.add_option('-s', '--sigma',      dest='sigma',       type='float',  default=3.,     help='reject points sigma stds away from the mean')
    parser.add_option('-p', '--processors', dest='processors',  type='int',    default=None,   help='number of processors to use')
    (options, args) = parser.parse_args()
    if options.interpolant in 'least_squares_polynomial':
        interpolant = interpolation.least_squares_polynomial
        evaluator = interpolation.polynomial_evaluator
    elif options.interpolant in 'spline':
        parser.error('Spline interpolation not yet implemented.')
        interpolant = interpolation.spline
        evaluator = interpolation.spline_evaluator
    elif options.interpolant in 'trigonometric':
        parser.error('Trigonometric interpolation not yet implemented.')
        interpolant = interpolation.trigonometric
        evaluator = interpolation.trigonometric_evaluator
    elif options.interpolant in 'piecewise_linear':
        parser.error('Piecewise linear interpolation not yet implemented.')
        interpolant = interpolation.piece_wise_linear
        evaluator = interpolation.piece_wise_linear_evaluator
    else:
        parser.error('Could not interpret ' + options.interpolant)
    return options, interpolant, evaluator

def lightcurve(file):
    from lightcurve import lightcurve
    return lightcurve(file,
                      interpolant,
                      evaluator,
                      options.degree,
                      options.min_period,
                      options.max_period,
                      options.period_bins,
                      options.sigma,
                      options.min_obs)

if __name__ == "__main__":
    from os import path, listdir
    from multiprocessing import Pool
    import matplotlib as plt
    options, interpolant, evaluator = get_options()
    star_files = (path.join(options.input, file)
                  for file in sorted(listdir(options.input))
                  if file[-4:] == options.format)
    if options.processors is None or options.processors > 1:
        if options.processors is None: p = Pool()
        else: p = Pool(options.processors)
        stars = p.map(lightcurve, star_files)
        p.close()
        p.join()
    else: stars = map(lightcurve, star_files)
    #for name, period, rephased, coefficients in stars:
    #    
    #    numpy.savetxt((numpy.array(star[0]
    # do something!